#include <iostream>
#include "utils/string_util.h"
#include "registerSubscriber.h"
#include "requestTopic.h"
#include "connect.h"
#include "utils/string_cast.h"
#include "unregisterSubscriber.h"

string parserTopicHost(const string &response)
{
    vector<std::string> split = StringUtil::split(response, "http://");
    vector<std::string> split1 = StringUtil::split(split[1], "/");
    return split1[0];
}

void parserPublisherHost(const string &response, string *host, int *port)
{
    *host = "192.168.101.101";
    auto vector = StringUtil::split(response, "<i4>");
    auto split1 = StringUtil::split(vector[2], "</i4>");
    *port = StringCast::toInt(split1[0]);
}

int main()
{
    string response = registerSubscriber();
    cout << response << endl;
    string host = parserTopicHost(response);
    cout << host << endl;

    response = requestTopic(host);
    cout << response << endl;
    string ip;
    int port;
    parserPublisherHost(response, &ip, &port);

    connect2Server(ip, port);

    response = unregisterSubscriber();
    cout << response << endl;

    cout << "done" << endl;
    return 0;
}
